package cc.comac.mom.util;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;

public class TimeUtil {

	public static Date getLocalDate() {

		ZoneId zoneId = ZoneId.systemDefault();

		ZonedDateTime zoneDateTime = getLocalDateTime().atZone(zoneId);

		return Date.from(zoneDateTime.toInstant());
	}

	public static LocalDateTime getLocalDateTime() {

		ZoneId zoneId = ZoneId.systemDefault();
		Instant instant = new Date().toInstant();

		return instant.atZone(zoneId).toLocalDateTime();
	}

	public static LocalDateTime getMaxLocalDateTime() {

		ZonedDateTime maxTime = ZonedDateTime.of(LocalDate.of(9999, 12, 30), LocalTime.MAX, ZoneId.systemDefault());

		return maxTime.toLocalDateTime();
	}

	public static String getLocalDateTimeStr() {

		DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

		return timeFormatter.format(getLocalDateTime());
	}

	/**
	 * 获取当前时区
	 **/
	public static String getLocalTimeZone() {
		return TimeZone.getDefault().getID();
	}
	
	// 将时间Data格式化成yyyy-MM-dd HH:mm:ss的字符串
	public static String fomatDate (Date date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format = simpleDateFormat.format(date);
		return format;
	}
	 /**
	  *  将LocalDateTime转换成Date
	  * **/
	public static Date fomatLocalDateTimeToDate (LocalDateTime date) {
		return Date.from(date.atZone(ZoneId.systemDefault()).toInstant());
	}
	 /**
	  *  将LocalDateTime转换成Date
	  * **/
		public static LocalDateTime fomatDateToLocalDateTime (Date date) {
			return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
		}
		/**
		  *  传入前后Date数据计算出两者之间的差值以小时计算
		  * **/
		public static  String subDate (Date startDate,Date endDate) {
			DecimalFormat decimalFormat=new DecimalFormat(".00");
			String format = decimalFormat.format((endDate.getTime()-startDate.getTime())/1000/60/60);
			return format;
		}
		
		public static Date stringToDaye (String date) throws ParseException {
			SimpleDateFormat simpleDateFormat = null;
			if (date.contains("T")) {
				simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:SS");
			} else {
				simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
			}
			Date parse = simpleDateFormat.parse(date);
			return parse;
		}
		
}
